<template>
  <div>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="部门管理">
        <el-input v-model="queryForm.kw" placeholder="部门名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column align="center" prop="deptName" label="部门名称">
      </el-table-column>
      <el-table-column align="center" prop="deptLeader.realName" label="部长">
      </el-table-column>
      <el-table-column prop="description" label="部门介绍" min-width="250">
      </el-table-column>
      <el-table-column align="center" label="操作" width="220">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row.deptId)">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleStatus(scope.row.deptId, -1)" v-if="scope.row.status == 1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" v-model:page-size="queryForm.pageSize" v-model:current-page="queryForm.pageNo" :total="total" @current-change="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog v-model="open" :title="title" width="800px" append-to-body>
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="formRef">
        <el-form-item label="部门名称" prop="deptName">
          <el-input
            v-model="editForm.deptName"
            auto-complete="off"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item label="部长" prop="leader">
          <el-select v-model="editForm.leader" placeholder="请选择部长" filterable>
            <el-option
              v-for="item in userList"
              :key="item.employeeId"
              :label="item.realName"
              :value="item.employeeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="description">
          <el-input
            v-model="editForm.description"
            auto-complete="off"
            placeholder=""
            type="textarea"
            :rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, nextTick, toRef } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { timestampToDayHM, timestampToHM } from "@/utils/utils"
import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();
// 查询参数
const queryForm = reactive({
  kw: "",
  pageNo: 1,
  pageSize: 10
});
// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);
// 列表返回值
const list = ref<any[]>([]);
const userList = ref<any[]>([]);
// 表单实例
const formRef = ref();
// 是否打开弹出框
const open = ref(false);
const openDetail = ref(false);
// 弹出框标题
const title = ref("");
const detailData = ref();
// 提交表单数据
let editForm = toRef(
  reactive({
    deptId: '',
    deptName: "",
    description: "",
    leader: ""
  })
);

// 表单验证
const rules = ref({
  deptName: [{ required: true, message: "不能为空", trigger: "blur" }],
  description: [{ required: true, message: "不能为空", trigger: "blur" }],
});

/**
 * 查询列表
 */
const getList = () => {
  loading.value = true;
  baseService
    .post("/dept/list", queryForm).then((res) => {
      loading.value = false;
      if (res.code === 200) {
        list.value = res.data.records;
        total.value = res.data.totalrecord;
      } else {
        list.value = [];
      }
    })
    .catch(() => {
      loading.value = false;
    });
};

/**
 * 搜索按钮操作
 */
function handleQuery() {
  queryForm.pageNo = 1;
  getList();
}

/**
 * 表单重置
 */
function reset() {
  editForm.value = {
    deptId: '',
    deptName: "",
    description: "",
    leader: ""
  };

  nextTick(() => {
    formRef.value?.resetFields();
  });
}

/**
 * 新增按钮操作
 */
function handleAdd() {
  reset();
  
  baseService.get(`/admin/user/getAll`).then((res) => {
    if(res.code==200){
      userList.value = res.data
    }
  })
  open.value = true;
  title.value = "添加";
}

/**
 * 修改按钮操作
 * @param id 主键
 */
function handleUpdate(id: string) {
  open.value = true;
  title.value = "修改";
  reset();
  baseService.get(`/dept/info/${id}`).then((res) => {
    if (res.code === 200) {
      editForm.value = res.data;
    }
  });
  baseService.get(`/admin/user/getAll`).then((res) => {
    if(res.code==200){
      userList.value = res.data
    }
  })
}

/**
 * 提交按钮
 */
function submitForm() {
  formRef.value.validate((valid: boolean) => {
    if (!valid) return;
    // editForm.value.pics = pics.join(',')
    baseService.post(`/dept/saveOrUpdate`, editForm.value).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        open.value = false;
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

/**
 * 删除按钮操作
 * @param id 主键
 */
function handleDelete(id: any) {
  ElMessageBox.confirm("确认要删除当前项吗?", "提示").then(() => {
    baseService.delete(`/dept/delete`, id).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

/**
 * 更新状态
 * @param id 
 */
function handleStatus(id: any, status: any) {
  let msg = '确定要删除吗？'
  ElMessageBox.confirm(msg, "提示").then(() => {
    baseService.delete(`/manager/update/status`, {deptId: id, status: status}).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

getList();
</script>

<style scoped>
.pointer{
  cursor: pointer;
}
.pointer:HOVER{
  color: #409eff;
}
.introduction{
width: 80%;
margin: 15px auto;
white-space: pre-line;
}
.addpic{
font-size: 22px;
padding: 20px 25px 15px 25px;
border: 2px dashed #ccc;
color: #888;
height: 68px;
}
.img-list{
display: flex;
flex-wrap: wrap;
}
.img-list .img-li{
width: 260px;
position: relative;
margin-right: 10px;
}
.logo-remove{
font-size: 22px;
position: absolute;
top: 10px;
right: 10px;
color: #f00;
cursor: pointer;
text-shadow: #000 4px 4px 5px;
}
.logo-remove .el-icon{
box-shadow: #fff 3px 5px 6px;
background-color: #fff;
}
.logo-remove:HOVER{
color: #F56C6C;
}
</style>
